package co.gov.medellin.ssti.sub.web.action;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.springframework.beans.factory.annotation.Autowired;

import co.gov.medellin.ssti.sub.common.excepcion.SUBException;
import co.gov.medellin.ssti.sub.common.util.Atributo;
import co.gov.medellin.ssti.sub.model.dao.exception.DAOException;
import co.gov.medellin.ssti.sub.model.service.AtributosService;
import co.gov.medellin.ssti.sub.model.service.ConsultasService;
import co.gov.medellin.ssti.sub.model.service.SubprogramaService;
import co.gov.medellin.ssti.sub.model.vo.SubprogramaVO;
import co.gov.medellin.ssti.sub.paoyer.administration.model.vo.UserVO;

public class BuscarAtributoAction extends Action {
	@Autowired
	AtributosService atributosServiceImpl;
	@Autowired
	ConsultasService consultasServiceImpl;
	@Autowired
	SubprogramaService subprogramaServiceImpl;

	/**
	 * This is the main action called from the Struts framework.
	 * 
	 * @param mapping
	 *            The ActionMapping used to select this instance.
	 * @param form
	 *            The optional ActionForm bean for this request.
	 * @param request
	 *            The HTTP Request we are processing.
	 * @param response
	 *            The HTTP Response we are processing.
	 * @return
	 * @throws java.io.IOException
	 * @throws javax.servlet.ServletException
	 */
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws IOException, ServletException {

		try {
			List<Atributo> lAtriSub = new ArrayList<Atributo>();
			SubprogramaVO objsub = new SubprogramaVO();
			HttpSession session = request.getSession();
			SubprogramaVO criterea = new SubprogramaVO();
			UserVO user = (UserVO) session.getAttribute("userInfo");

			Collection<SubprogramaVO> lSubprog = subprogramaServiceImpl
					.findSubprogramaByCriteria(criterea, user.getCode(),
							user.getUserGroupName(), user.getCodeArea());

			Collection<SubprogramaVO> listaTamano = new ArrayList<SubprogramaVO>();

			SubprogramaVO objsubproVO = new SubprogramaVO();
			String codsubprograma = request.getParameter("codigo");
			if (codsubprograma == null) {
				codsubprograma = (String) session.getAttribute("codigo");
				session.removeAttribute("codigo");
			}
			objsubproVO.setCode(new Long(codsubprograma));

			boolean estado = subprogramaServiceImpl
					.findEstadoSubprogramaByCodigo(objsubproVO.getCode());

			if (estado) {
				session.setAttribute("mensajes",
						"El beneficio esta desactivado, no se pueden ver los atributos");
				return mapping.findForward("fail");
			}

			boolean sedeBeneficio = subprogramaServiceImpl
					.isRelacionadoConSede(new Long(codsubprograma));

			if (!sedeBeneficio) {
				String nameBeneficio = subprogramaServiceImpl
						.findNombreByCodigo(new Long(codsubprograma));
				String codPrograma = subprogramaServiceImpl
						.findCodigoProgramaByCodigoSubprograma(new Long(
								codsubprograma));
				request.setAttribute("codSubprograma", codsubprograma);
				request.setAttribute("codPrograma", codPrograma);
				session.setAttribute("codigo", codsubprograma);
				request.setAttribute(
						"msg",
						"El beneficio "
								+ nameBeneficio
								+ " no se encuentra seleccionado en la lista, debido a que no esta asociado a ninguna sede");
			}

			List<SubprogramaVO> lista = (List<SubprogramaVO>) lSubprog;

			if (lSubprog.size() > 0) {
				objsub = (SubprogramaVO) lista.get(0);
				listaTamano.add(objsub);
				session.setAttribute("listatam", listaTamano);
				session.setAttribute("subprogramas", lSubprog);
			} else {
				session.removeAttribute("listatam");
				session.removeAttribute("subprogramas");
				session.setAttribute("mensajes",
						"Verifique si el usuario se encuentra asociado al beneficio");
				return mapping.findForward("fail");
			}

			String strTipo = request.getParameter("accion");

			if (strTipo != null) {
				session.setAttribute("subpro", request.getParameter("codigo"));
				int opcion = 1;
				String strSubprograma = request.getParameter("codigo");
				if ((Object) strSubprograma == null) {
					strSubprograma = (String) session.getAttribute("codsubpro");
				}
				try {
					int conversion = atributosServiceImpl
							.findAtributosValidarLista(new Long(strSubprograma));
				} catch (DAOException e) {
					SUBException.logDebugError("", e);
				}
				session.setAttribute("codsubpro", strSubprograma);

				lAtriSub = (List<Atributo>) consultasServiceImpl
						.findAtributoEspecificos(strSubprograma, opcion);

				request.setAttribute("tamanoatri", lAtriSub.size() + "");
				session.setAttribute("atrisubprog", lAtriSub);

			}
		} catch (DAOException e) {
			SUBException.logDebugError("", e);
		}

		return mapping.findForward("success");
	}
}